﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TimHeuer.Expression.S3Publish.S3Model;
using System.Windows.Media.Animation;
using System.Threading;
using System.Windows.Threading;

namespace TimHeuer.Expression.S3Publish
{
    /// <summary>
    /// Interaction logic for StandardSettings.xaml
    /// </summary>
    public partial class StandardSettings : UserControl
    {
        private PublishData _publishData;
        private Publish _plugin;
        private ThreeSharpWrapper _s3;

        private delegate void NoArgDelegate();
        private delegate void SingleArgDelegate(String arg);

        private Storyboard authWaiting;
        private Storyboard authFailed;
        private Storyboard authSuccess;
        private Storyboard authNoAuth;

        public StandardSettings(PublishData data, Publish plugin)
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(StandardSettings_Loaded);
            _publishData = data;
            _plugin = plugin;
            base.DataContext = data;
        }

        void StandardSettings_Loaded(object sender, RoutedEventArgs e)
        {
            authWaiting = (Storyboard)this.Resources["Authentication_Waiting"];
            authFailed = (Storyboard)this.Resources["Authentication_Failed"];
            authSuccess = (Storyboard)this.Resources["Authentication_Succeeded"];
            authNoAuth = (Storyboard)this.Resources["Authentication_NotAuthenticated"];
        }

        private void authenticate_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_publishData.AccessKey) || string.IsNullOrEmpty(_publishData.SecretKey))
            {
                MessageBox.Show("You need to enter your Amazon S3 account information before proceeding", "Account Information Needed", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                return;
            }

            NoArgDelegate fetcher = new NoArgDelegate(this.AuthAndListBuckets);
            fetcher.BeginInvoke(null, null);
        }

        private void AuthAndListBuckets()
        {
            Dispatcher.BeginInvoke(new NoArgDelegate(authWaiting.Begin), null);

            try
            {
                Dispatcher.BeginInvoke(new NoArgDelegate(ListBuckets), null);
            }
            catch (Exception ex)
            {
                Dispatcher.BeginInvoke(new NoArgDelegate(authFailed.Begin), null);
                MessageBox.Show(ex.Message, "Authentication Failed", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ListBuckets()
        {
            if (_s3 == null)
            {
                _s3 = new ThreeSharpWrapper(_publishData.AccessKey, _publishData.SecretKey);
            }
            IList<S3Bucket> buckets = _s3.ListAllBuckets();
            Dispatcher.BeginInvoke(new NoArgDelegate(authSuccess.Begin), null);
            if (buckets != null)
            {
                BucketList.Items.Clear();
                foreach (var bucket in buckets)
                {
                    BucketList.Items.Add(bucket.Name);
                }
            }
        }
    }
}
